package gosnoopers.fr;

import java.util.ArrayList;
import java.util.HashMap;

import lazy_listview.JSONfunctions;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import common.Common;

public class ContactActivity extends Activity {
	

	//***********************check connect********************************
	public boolean isNetworkAvailable() {
		ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetworkInfo = connectivityManager
				.getActiveNetworkInfo();
		return activeNetworkInfo != null && activeNetworkInfo.isConnected();
	}
	//****************************************************************
	public String name = "";
	public String phone = "";
	public String title = "";
	public String message = "";
	public String to = "";
	
	public static String cvid = "";
	//*********************************CREATE**********************************
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);		
		setContentView(R.layout.contact);
		header();
		
		TextView text_view_landing_page_header = (TextView) findViewById(R.id.text_view_landing_page_header);
        text_view_landing_page_header.setText("Send message");
		
        ImageView btn_setting = (ImageView) findViewById(R.id.image_setting);
		btn_setting.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent goToNextActivity = new Intent(getApplicationContext(), Settings.class);
	            startActivity(goToNextActivity);
			}
		});
		
        Intent iin= getIntent();
        Bundle b = iin.getExtras();
        cvid  = "";
        to = "";
        if(b!=null)
        {
        	cvid =(String) b.get("VICTIME_ID");
        	to = (String) b.get("EMAIL");
            
        }
        
        
        Log.i("CONTACT VIC ID ",cvid);
        Log.i("RRRRRRRRRRRRRRRRMAI ",to);
        
		Button  btn_send_message = (Button) findViewById(R.id.btn_send_message);
		btn_send_message.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				EditText edname = (EditText) findViewById(R.id.name);
				name = edname.getText().toString();
				
				EditText edphone = (EditText) findViewById(R.id.phone);
				phone = edname.getText().toString();
				
				EditText edtitle = (EditText) findViewById(R.id.title);
				title = edtitle.getText().toString();
				
				EditText edmessage = (EditText) findViewById(R.id.message);
				message = edmessage.getText().toString();
				
				if(to.equalsIgnoreCase("")){
					Toast toast = Toast.makeText(getApplicationContext(),
							"You can't send message, email author is blank", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 30, 30);
					toast.show();
				}else if(name.equalsIgnoreCase("")){
					Toast toast = Toast.makeText(getApplicationContext(),
							"Name not blank", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 30, 30);
					toast.show();
					
				}else if(phone.equalsIgnoreCase("")){
					Toast toast = Toast.makeText(getApplicationContext(),
							"Phone not blank", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 30, 30);
					toast.show();
					
				}else if(title.equalsIgnoreCase("")){
					Toast toast = Toast.makeText(getApplicationContext(),
							"Title not blank", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 30, 30);
					toast.show();
					
				}else if(message.equalsIgnoreCase("")){
					Toast toast = Toast.makeText(getApplicationContext(),
							"Message not blank", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 30, 30);
					toast.show();
					
				}else{
					// check network 
					if (!isNetworkAvailable()) {

						Toast toast = Toast.makeText(getApplicationContext(),
								Common.MSG, Toast.LENGTH_SHORT);
						toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 30, 30);
						toast.show();
					} else {
						new Save().execute();
					}
					
				}
				
				
			}
		});
		
	}
	
	//******************************HEADER***************************************
		public void header() {
			ImageView btn_setting = (ImageView) findViewById(R.id.image_setting);
			btn_setting.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent goToNextActivity = new Intent(getApplicationContext(),
							Settings.class);
					startActivity(goToNextActivity);
				}
			});

			/**
			 * Kiem user da dang nhap hay chua 1. hein thi icon user 2. hien thi
			 * icon add victimes
			 */
			ImageView image_open_left_navigation = (ImageView) findViewById(R.id.image_open_left_navigation);
			ImageView icon_header_form = (ImageView) findViewById(R.id.icon_header_form);
			if (MainActivity.userId.equalsIgnoreCase("0")) {
				image_open_left_navigation.setVisibility(View.VISIBLE);
				icon_header_form.setVisibility(View.GONE);
			} else {
				image_open_left_navigation.setVisibility(View.GONE);
				icon_header_form.setVisibility(View.VISIBLE);
			}

			image_open_left_navigation.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent goToNextActivity = new Intent(getApplicationContext(),
							MainActivity.class);
					startActivity(goToNextActivity);
				}
			});

			icon_header_form.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent goToNextActivity = new Intent(getApplicationContext(),
							FormFirst.class);
					startActivity(goToNextActivity);
				}
			});
		}
	
	//********************************************************************
	
	ArrayList<HashMap<String, String>> arraylist;
	static String ID = "id";
	static String NAME = "name";
	static String ALIAS = "alias";
	public static JSONObject jsonobject;
	public static JSONArray jsonarray;
	
	ListView list;
	boolean checkError = false;
	String mes = "";
	
	
	// **********************DownloadJSON AsyncTask******************************
		private class Save extends AsyncTask<Void, Void, Void> {

			@Override
			protected void onPreExecute() {
				super.onPreExecute();
				ProgressBar progressbar_sign_up = (ProgressBar) findViewById(R.id.progressbar_sign_up);
				progressbar_sign_up.setVisibility(View.VISIBLE);
			}

			@Override
			protected Void doInBackground(Void... params) {
				// Create an array
				arraylist = new ArrayList<HashMap<String, String>>();
				// Retrieve JSON Objects from the given URL address

				String paramsURL = "to="
						+ to
						+ "&idvictem="
						+ cvid
						+ "&name="
						+ name
						+ "&phone="
						+ phone
						+ "&name="
						+ name
						+ "&subject="
						+ title
						+ "&message="
						+ message;
				
				Log.d("PARAMS CONTACT", "Json get post: " + paramsURL);
				jsonobject = JSONfunctions.executeUrlWithOption(true,"http://gosnoopers.com/victimes/app/sendMessage", paramsURL, false);
				Log.d("FORM FIRST", "Json get post: " + jsonobject);
				try {
					
					
					checkError = jsonobject.getBoolean("error");
					mes = jsonobject.getString("message");
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}

			@Override
			protected void onPostExecute(Void args) {
				ProgressBar progressbar_sign_up = (ProgressBar) findViewById(R.id.progressbar_sign_up);
				progressbar_sign_up.setVisibility(View.GONE);
				if(!checkError){
					ContactActivity.this.finish();
				}else{
					
					Toast toast = Toast.makeText(getApplicationContext(),
							mes, Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 30, 30);
					toast.show();
				}
			}
		}
	
	//****************************on resume***************************
	@Override
	protected void onResume() {
		super.onResume();
		EditText edname = (EditText) findViewById(R.id.name);
		edname.setText("");
		
		EditText edphone = (EditText) findViewById(R.id.phone);
		edphone.setText("");
		
		EditText edtitle = (EditText) findViewById(R.id.title);
		edtitle.setText("");
		
		EditText edmessage = (EditText) findViewById(R.id.message);
		edmessage.setText("");
		
		
	}

	

}